package com.mzy.linear_struct.ks.回溯;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: mizy3
 * @Date: 2024/07/26/上午9:21
 * @Description:
 */
public class Pro46全排列 {

    public List<List<Integer>> permute(int[] nums) {

        List<List<Integer>> res = new ArrayList<>();
        dfs(nums, new boolean[nums.length], new LinkedList<Integer>(), 0, res, nums.length);
        return res;
    }


    public void dfs(int[] nums, boolean[] used, Deque<Integer> path, int depth,
                    List<List<Integer>> res, int len) {
        if (len == depth) {
            res.add(new ArrayList<>(path));
            return;
        }

        for (int i = 0; i < len; i++) {
            if(!used[i]){
                path.addLast(nums[i]);
                used[i] = true;
                dfs(nums, used, path, depth + 1, res, len);
                used[i] = false;
                path.removeLast();
            }
        }
    }
}












